package com.zyzd.commontools.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.zyzd.commontools.R;

import java.util.Objects;

/**
 * Created by lzy on 2018/6/4 0004.
 * description ${dialog基类}
 */
public abstract class BaseDialog extends Dialog {
    public BaseDialog(@NonNull Context context) {
        this(context, R.style.common_dialog_base);
    }

    public BaseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);

        // 去掉标题，必须在setContentView之前调用
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View contentView = getLayoutInflater().inflate(getLayoutId(), null);
        initView(contentView);
        setContentView(contentView);
        // 设置Dialog的宽度等于屏幕的宽度，必须在setContentView后调用
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = getWidth();
        p.height = getHeight();
        p.gravity = getGravity();
        getWindow().setAttributes(p);
        getWindow().setWindowAnimations(getWindowAnimations());

        hideNavigation();

        // 让Dialog显示在底部
        getWindow().setGravity(Gravity.CENTER);
    }

    private void hideNavigation() {
        if (!isHideNavigation()) return;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
        Objects.requireNonNull(getWindow()).getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        );
    }

    private boolean isHideNavigation() {
        return false;
    }

    protected int getWindowAnimations() {
        return R.style.common_anim_bottom;
    }

    protected abstract int getLayoutId();

    protected abstract void initView(View contentView);

    protected abstract int getWidth();

    protected abstract int getHeight();

    protected int getGravity() {
        return Gravity.CENTER_VERTICAL;
    }
}
